package ${package}.config;

import com.company.nuwa.common.handler.ThreadPoolHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * <p>所有线程池都执行队列满了抛弃策略</p>
 *
 * @author Lionel Lee
 * @version 1.0: ThreadPoolConfig.java
 * @date 2021/4/8 3:41 下午
 */
@Configuration
@EnableAsync
public class ThreadPoolConfig {


    /**
     * 默认线程池
     *
     * @return TaskExecutor
     */
    @Bean
    public TaskExecutor taskExecutor() {
        return buildExecutor(Runtime.getRuntime().availableProcessors(),
                Runtime.getRuntime().availableProcessors() * 2, 200, 60, "default-taskExecutor-");
    }

    private ThreadPoolTaskExecutor buildExecutor(int coreSize, int maxSize, int queueSize,
                                                 int keepSeconds, String namePrefix) {
        return ThreadPoolHandler.build(
                namePrefix,
                keepSeconds,
                queueSize,
                coreSize,
                maxSize,
                ThreadPoolHandler.RejectedHandlerStrategy.CALLER
        );
    }

}
